#include "../VisaSerial.h"
#include <UnitTest++.h>
#include "visa.h"
#include <iostream>
#include <string.h>

//~ TEST(FailSpectacularly)
//~ {
    //~ CHECK(false);
//~ }

TEST(VisaSerialPassAlways)
{
    CHECK(true);
}

TEST(ResourceMangerClassCreation)
{
	ResourceManager rm;
}

TEST(InstrumentClassCreationDefault)
{
	Instrument instrum;
}
TEST(InstrumentClassCreationWithName)
{
	Instrument instrum("ASRL1::INSTR");
}

TEST(InstrumentClassCreationWithInvalidName)
{
	CHECK_THROW( Instrument instrum("xyz::ABC"), VISAException);
}

TEST(SetInstrumentAttributes)
{
	Instrument instrum("ASRL1::INSTR");
	instrum.setAttribute(VI_ATTR_TMO_VALUE, 5000);
	instrum.setAttribute (VI_ATTR_ASRL_BAUD, 4800);
	instrum.setAttribute (VI_ATTR_ASRL_DATA_BITS, 8);  // 5-8 bit allowed
}

TEST(SetInstrumentInvalidAttribute)
{
	Instrument instrum("ASRL1::INSTR");
	instrum.setAttribute (VI_ATTR_ASRL_BAUD, 4801);	// should fail somewhere
	CHECK_THROW(instrum.setAttribute (VI_ATTR_ASRL_DATA_BITS, 4), VISAException); // 5-8 bit allowed
}

TEST(InstrumentWrite)
{
	Instrument in("ASRL1::INSTR");
	char m1[] = "Hello Serial Port from Insturment class";
	CHECK_EQUAL(in.write(m1),	strlen(m1));  // returns number of chars written
	std::string m2 = "String hello";
	in.write((char *) m2.c_str());  // Yuck
}

TEST(InstrumentWriteString)
{
	Instrument in("ASRL1::INSTR");
	std::string m2 = "String hello";
	CHECK_EQUAL(in.write(m2),m2.size());
}

TEST(InstrumentRead)
{
	Instrument in("ASRL1::INSTR");
	char m1[] = "Hello Serial Port from Insturment Class";
	in.write(m1);
	CHECK_EQUAL(in.read(strlen(m1)) , m1);
}

TEST(InstrumentReadStrings)
{
	Instrument in("ASRL1::INSTR");
	std::string msg = "Hello Serial Port from Insturment Class";
	in.write(msg);
	CHECK_EQUAL(in.read(msg.size()) , msg);
}

TEST(InstrumentReadStringsTimeoutIsErrorSwitch)
{
	Instrument in("ASRL1::INSTR");
	in.setAttribute(VI_ATTR_TMO_VALUE, 40);
	std::string msg = "Hello Serial Port from Insturment Class";
	in.write(msg);
	CHECK_THROW(in.read(msg.size()+10, true) , VISAException);
	in.write(msg);
	CHECK_EQUAL(in.read(msg.size()+10, false) , msg);
}
